﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TDLib.Web
{
    /// <summary>
    /// 页面处理类
    /// </summary>
    public class PageHelper
    {

#if NET_2_0
        /// <summary>
        /// 获取指定长度字符
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string GetShortStr<T>(T str, int len)
        {
            string tmp = str.ToString();

            if (tmp.Length > len - 2)
            {
                tmp = tmp.Substring(0, len - 2) + "...";
            }

            return tmp;
        }

        /// <summary>
        /// 把日期转换成短日期
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetShortTime<T>(T str)
        {
            return FormatTime(str, "yyyy-MM-dd");
        }

        /// <summary>
        /// 转换日期显示格式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string FormatTime<T>(T str,string format)
        {
            DateTime tmp = Convert.ToDateTime(str.ToString());
            return tmp.ToString(format);
        }
#endif
#if NET_1_1
        /// <summary>
        /// 获取指定长度字符
        /// </summary>
        /// <typeparam name="Object"></typeparam>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string GetShortStr(Object str, int len)
        {
            string tmp = str.ToString();

            if (tmp.Length > len - 2)
            {
                tmp = tmp.Substring(0, len - 2) + "...";
            }

            return tmp;
        }

        /// <summary>
        /// 把日期转换成短日期
        /// </summary>
        /// <typeparam name="Object"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetShortTime(Object str)
        {
            return FormatTime(str, "yyyy-MM-dd");
        }

        /// <summary>
        /// 转换日期显示格式
        /// </summary>
        /// <typeparam name="Object"></typeparam>
        /// <param name="str"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string FormatTime(Object str,string format)
        {
            DateTime tmp = Convert.ToDateTime(str.ToString());
            return tmp.ToString(format);
        }
#endif

        /// <summary>
        /// 页面处理类
        /// </summary>
        /// <param name="ParaName"></param>
        /// <param name="ParaRec">没有此参数时返回-1</param>
        public static void GetRequestPara(string ParaName, ref int ParaRec)
        {
            if (HttpContext.Current.Request[ParaName] != null)
            {
                string ParaStr = HttpContext.Current.Request[ParaName];
                try
                {
                    ParaRec = int.Parse(ParaStr);
                }
                catch
                {
                    
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ParaName"></param>
        /// <param name="ParaRec">没有此参数则不改变</param>
        public static void GetRequestPara(string ParaName, ref string ParaRec)
        {
            if (HttpContext.Current.Request[ParaName] != null)
            {
                ParaRec = HttpContext.Current.Request[ParaName].Trim();
            }
        }

        /// <summary>
        /// 获取Repeater、DataList、DataGrid数据中选中的内容
        /// </summary>
        /// <param name="checkCtrlName"></param>
        /// <param name="labelCtrlName"></param>
        /// <param name="dataCtrl"></param>
        /// <returns>选中控件的ID号组，如1,2,3,4</returns>
        public static string GetCtrlId(string checkCtrlName, string labelCtrlName, Control dataCtrl)
        {
            return GetCtrlId(checkCtrlName, labelCtrlName, dataCtrl, "|");
        }

        public static string GetCtrlId(string checkCtrlName, string labelCtrlName, Control dataCtrl, string splitChar)
        {
            string strRet = "";
            int j = 1;

        
            //＝＝＝Repeater＝＝＝
            if (dataCtrl is Repeater)
            {
                for (int i = 0; i < ((Repeater)dataCtrl).Items.Count; i++)
                {
                    CheckBox myCheckBox;
                    Label myLabel;
                    myCheckBox = (CheckBox)((Repeater)dataCtrl).Items[i].FindControl(checkCtrlName);
                    myLabel = (Label)((Repeater)dataCtrl).Items[i].FindControl(labelCtrlName);
                    if (myCheckBox.Checked == true)
                    {
                        if (j == 1)
                        {
                            strRet = myLabel.Text;
                        }
                        else
                        {
                            strRet = strRet + splitChar + myLabel.Text;
                        }
                        j = j + 1;
                    }
                }
            }
            //＝＝＝DataList＝＝＝
            if (dataCtrl is DataList)
            {
                for (int i = 0; i < ((DataList)dataCtrl).Items.Count; i++)
                {
                    CheckBox myCheckBox;
                    Label myLabel;
                    myCheckBox = (CheckBox)((DataList)dataCtrl).Items[i].FindControl(checkCtrlName);
                    myLabel = (Label)((DataList)dataCtrl).Items[i].FindControl(labelCtrlName);
                    if (myCheckBox.Checked == true)
                    {
                        if (j == 1)
                        {
                            strRet = myLabel.Text;
                        }
                        else
                        {
                            strRet = strRet + splitChar + myLabel.Text;
                        }
                        j = j + 1;
                    }
                }
            }
            //＝＝＝DataGrid＝＝＝
            if (dataCtrl is DataGrid)
            {
                for (int i = 0; i < ((DataGrid)dataCtrl).Items.Count; i++)
                {
                    CheckBox myCheckBox;
                    Label myLabel;
                    myCheckBox = (CheckBox)((DataGrid)dataCtrl).Items[i].FindControl(checkCtrlName);
                    myLabel = (Label)((DataGrid)dataCtrl).Items[i].FindControl(labelCtrlName);
                    if (myCheckBox.Checked == true)
                    {
                        if (j == 1)
                        {
                            strRet = myLabel.Text;
                        }
                        else
                        {
                            strRet = strRet + splitChar + myLabel.Text;
                        }
                        j = j + 1;
                    }
                }
            }
        
            return strRet;
        }


    }
}
